package com.codeoss.module.product.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.dto.AttributeDTO;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.util.ValueNamePair;
import com.codeoss.module.product.model.ProductAttribute;
import com.codeoss.module.product.model.ProductAttributeInstance;
import com.codeoss.module.product.model.ProductAttributeSet;
import com.codeoss.module.product.model.ProductAttributeSetInstance;
import com.codeoss.module.product.model.ProductAttributeValue;
import com.codeoss.module.product.model.ProductProduct;
import com.codeoss.module.product.response.AttributeSetInstanceComponentResponse;
import com.codeoss.module.product.service.IAttributeSetInstanceComponentService;
import com.codeoss.module.product.service.IProductAttributeInstanceService;
import com.codeoss.module.product.service.IProductAttributeService;
import com.codeoss.module.product.service.IProductAttributeSetInstanceService;
import com.codeoss.module.product.service.IProductAttributeSetService;
import com.codeoss.module.product.service.IProductAttributeValueService;
import com.codeoss.module.product.service.IProductProductService;

@Service
public class AttributeSetInstanceComponentService implements IAttributeSetInstanceComponentService {
	@Autowired
	private IProductAttributeService productAttributeService;
	@Autowired
	private IProductAttributeValueService productAttributeValueService;
	@Autowired
	private IProductAttributeSetInstanceService productAttributeSetInstanceService;
	@Autowired
	private IProductAttributeInstanceService productAttributeInstanceService;
	@Autowired
	private IProductProductService productProductService;
	@Autowired
	private IProductAttributeSetService productAttributeSetService;

	@Override
	public AttributeSetInstanceComponentResponse init(String pageCode, Long productProductId, Long productAttributeSetId,
			Long productAttributeSetInstanceId) {
		// TODO Auto-generated method stub
		AttributeSetInstanceComponentResponse attributeSetInstanceResponse = new AttributeSetInstanceComponentResponse();
		List<AttributeDTO> attributeDTOList = new ArrayList<AttributeDTO>();
		attributeSetInstanceResponse.setAttributeDTOList(attributeDTOList);

		Long oldProductAttributeSetInstanceId=productAttributeSetInstanceId;
		boolean isProductPage="product_product_formview".equals(pageCode) || "mobile_product_product_formview".equals(pageCode);
		if (!isProductPage) {
			ProductProduct productProduct = productProductService.getById(productProductId);
			productAttributeSetId = productProduct.getProductAttributeSetId();
			oldProductAttributeSetInstanceId=productProduct.getProductAttrSetInstanceId();
		}
		
		List<ProductAttributeInstance> productAttributeInstanceList = null;
		if (oldProductAttributeSetInstanceId != null) {
			productAttributeInstanceList = productAttributeInstanceService
					.getAttributeInstanceListByAttributeSetInstanceId(oldProductAttributeSetInstanceId);
		}
				
		List<ProductAttribute> productAttributeList = productAttributeService
				.getProductAttributeListByAttributeSetId(productAttributeSetId);
		for (int i = 0; i < productAttributeList.size(); i++) {
			ProductAttribute productAttribute = productAttributeList.get(i);
			AttributeDTO attributeDTO = new AttributeDTO();
			attributeDTOList.add(attributeDTO);
			attributeDTO.setAttributeId(productAttribute.getId() + "");
			attributeDTO.setLabel(productAttribute.getName());
			String attributeValueType = productAttribute.getAttributeValueType();
			if ("list".equals(attributeValueType)) {
				attributeDTO.setType("list");
				List<ProductAttributeValue> productAttributeValueList = productAttributeValueService
						.getProductAttributeValueListByAttributeId(productAttribute.getId());
				List<ValueNamePair> pairList = new ArrayList<ValueNamePair>();
				attributeDTO.setOptionList(pairList);
				for (int j = 0; j < productAttributeValueList.size(); j++) {
					ProductAttributeValue productAttributeValue = productAttributeValueList.get(j);
					ValueNamePair pair = new ValueNamePair(productAttributeValue.getName(),
							productAttributeValue.getCode());
					pairList.add(pair);
				}
				if (productAttributeInstanceList != null) {
					for (int j = 0; j < productAttributeInstanceList.size(); j++) {
						ProductAttributeInstance productAttributeInstance = productAttributeInstanceList.get(j);
						if (productAttributeInstance.getProductAttributeId().longValue() == productAttribute.getId()
								.longValue()) {
							Long productAttributeValueId = productAttributeInstance.getProductAttributeValueId();
							ProductAttributeValue productAttributeValue = productAttributeValueService
									.getById(productAttributeValueId);
							attributeDTO.setValue(productAttributeValue.getCode());
							break;
						}
					}
				}
			} else if ("string".equals(attributeValueType)) {
				attributeDTO.setType("string");
				if (productAttributeInstanceList != null) {
					for (int j = 0; j < productAttributeInstanceList.size(); j++) {
						ProductAttributeInstance productAttributeInstance = productAttributeInstanceList.get(j);
						if (productAttributeInstance.getProductAttributeId().longValue() == productAttribute.getId()
								.longValue()) {
							attributeDTO.setValue(productAttributeInstance.getValue());
							break;
						}
					}
				}
			} else if ("number".equals(attributeValueType)) {
				attributeDTO.setType("number");
				if (productAttributeInstanceList != null) {
					for (int j = 0; j < productAttributeInstanceList.size(); j++) {
						ProductAttributeInstance productAttributeInstance = productAttributeInstanceList.get(j);
						if (productAttributeInstance.getProductAttributeId().longValue() == productAttribute.getId().longValue()) {
							attributeDTO.setValue(productAttributeInstance.getValueNumber());
							break;
						}
					}
				}
			}
		}
		
		if (!isProductPage) {
			ProductAttributeSet productAttributeSet = productAttributeSetService.getById(productAttributeSetId);
			ProductAttributeSetInstance productAttributeSetInstance=null;
			if(productAttributeSetInstanceId!=null) {
				productAttributeSetInstance=productAttributeSetInstanceService.getById(productAttributeSetInstanceId);
			}
			if (productAttributeSet.getLot()) {
				attributeSetInstanceResponse.setLot(true);
				if(productAttributeSetInstance!=null) {
					attributeSetInstanceResponse.setLotValue(productAttributeSetInstance.getLotNo());
				}
			}
			if(productAttributeSet.getSerNo()) {
				attributeSetInstanceResponse.setSerNo(true);
				if(productAttributeSetInstance!=null) {
					attributeSetInstanceResponse.setSerNoValue(productAttributeSetInstance.getSerNo());
				}
			}
		}
		return attributeSetInstanceResponse;
	}

	@Override
	@Transactional
	public AttributeSetInstanceComponentResponse dataSave(String pageCode, Long productProductId, Long productAttributeSetId,
			Long productAttributeSetInstanceId, Map<String, String> paraMap) {
		// TODO Auto-generated method stub
		AttributeSetInstanceComponentResponse attributeSetInstanceResponse = new AttributeSetInstanceComponentResponse();

		if(productAttributeSetId==null) {
			ProductProduct productProduct = productProductService.getById(productProductId);
			productAttributeSetId = productProduct.getProductAttributeSetId();
		}
		ProductAttributeSetInstance productAttributeSetInstance = null;
		String lotValue=paraMap.get("lotValue");
		String serNoValue=paraMap.get("serNoValue");
		StringBuilder descriptionSB = new StringBuilder();
		
		boolean isProductPage="product_product_formview".equals(pageCode);
		if (!isProductPage) {
			ProductAttributeSet productAttributeSet = productAttributeSetService.getById(productAttributeSetId);
			if (productAttributeSet.getLot()) {
				if(!StringUtils.isEmpty(lotValue)) {
					descriptionSB.append("批次号_" + lotValue + "_");
					QueryWrapper<ProductAttributeSetInstance> query=new QueryWrapper<ProductAttributeSetInstance>();
					query.eq("product_attribute_set_id", productAttributeSetId);
					query.eq("lot_no", lotValue);
					productAttributeSetInstance=productAttributeSetInstanceService.getOne(query);
				}
			}
            if (productAttributeSet.getSerNo()) {
				if(!StringUtils.isEmpty(serNoValue)) {
					descriptionSB.append("序列号_" + serNoValue + "_");
					if(productAttributeSetInstance!=null) {
						QueryWrapper<ProductAttributeSetInstance> query=new QueryWrapper<ProductAttributeSetInstance>();
						query.eq("product_attribute_set_id", productAttributeSetId);
						query.eq("ser_no", serNoValue);
						productAttributeSetInstance=productAttributeSetInstanceService.getOne(query);
					}	
				}
			}
		}
		if(productAttributeSetInstance==null) {
			if (productAttributeSetInstanceId != null) {
				productAttributeSetInstance = productAttributeSetInstanceService.getById(productAttributeSetInstanceId);
				productAttributeSetId = productAttributeSetInstance.getProductAttributeSetId();
			} else {
				productAttributeSetInstance = new ProductAttributeSetInstance();
				productAttributeSetInstance.setProductAttributeSetId(productAttributeSetId);
				productAttributeSetInstanceService.insert(productAttributeSetInstance);
			}
		}
		productAttributeSetInstance.setLotNo(lotValue);
		productAttributeSetInstance.setSerNo(serNoValue);
		
		List<ProductAttribute> productAttributeList = productAttributeService
				.getProductAttributeListByAttributeSetId(productAttributeSetId);

		for (int i = 0; i < productAttributeList.size(); i++) {
			ProductAttribute productAttribute = productAttributeList.get(i);
			String attributeValueType = productAttribute.getAttributeValueType();
			ProductAttributeInstance productAttributeInstance = productAttributeInstanceService
					.getAttributeInstanceByAttributeIdAndAttributeSetInstanceId(productAttribute.getId(),productAttributeSetInstance.getId());
			String sValue = paraMap.get(productAttribute.getId()+"");
			if ("list".equals(attributeValueType)) {
				List<ProductAttributeValue> productAttributeValueList = productAttributeValueService
						.getProductAttributeValueListByAttributeId(productAttribute.getId());
				for (int j = 0; j < productAttributeValueList.size(); j++) {
					ProductAttributeValue productAttributeValue = productAttributeValueList.get(j);
					if (productAttributeValue.getCode().equals(sValue)) {
						if (productAttributeInstance == null) {
							productAttributeInstance = new ProductAttributeInstance();
							productAttributeInstance.setProductAttrSetInstanceId(productAttributeSetInstance.getId());
							productAttributeInstance.setProductAttributeId(productAttribute.getId());
							productAttributeInstanceService.insert(productAttributeInstance);
						}
						productAttributeInstance.setProductAttributeValueId(productAttributeValue.getId());
						productAttributeInstanceService.update(productAttributeInstance);
						descriptionSB.append(productAttributeValue.getName() + "_");
					}
				}
			} else if ("string".equals(attributeValueType)) {
				if (productAttributeInstance == null) {
					productAttributeInstance = new ProductAttributeInstance();
					productAttributeInstance.setProductAttrSetInstanceId(productAttributeSetInstance.getId());
					productAttributeInstance.setProductAttributeId(productAttribute.getId());
					productAttributeInstanceService.insert(productAttributeInstance);
				}
				productAttributeInstance.setValue(sValue);
				productAttributeInstanceService.update(productAttributeInstance);
				descriptionSB.append(sValue + "_");
			} else if ("number".equals(attributeValueType)) {
				BigDecimal valueNumber = null;
				if (!StringUtils.isEmpty(sValue)) {
					valueNumber = new BigDecimal(sValue);
				}
				if (productAttributeInstance == null) {
					productAttributeInstance = new ProductAttributeInstance();
					productAttributeInstance.setProductAttrSetInstanceId(productAttributeSetInstance.getId());
					productAttributeInstance.setProductAttributeId(productAttribute.getId());
					productAttributeInstanceService.insert(productAttributeInstance);
				}
				productAttributeInstance.setValueNumber(valueNumber);
				productAttributeInstanceService.update(productAttributeInstance);
				if (valueNumber != null) {
					descriptionSB.append(sValue + "_");
				} else {
					descriptionSB.append(" _");
				}
			}
		}
		productAttributeSetInstance.setDescription(descriptionSB.toString());
		productAttributeSetInstanceService.update(productAttributeSetInstance);
		attributeSetInstanceResponse.setAttributeSetInstanceId(productAttributeSetInstance.getId());
		attributeSetInstanceResponse.setDescription(descriptionSB.toString());
		return attributeSetInstanceResponse;
	}
}
